import 'package:auto_route/auto_route.dart';
import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/common/widget/common_app_bar.dart';
import 'package:casdon/common/widget/primary_button.dart';
import 'package:casdon/common/widget/primary_check_box.dart';
import 'package:casdon/core/auth/auth_storage.dart';
import 'package:casdon/auto_router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

@RoutePage()
class DelAccountScreen extends ConsumerStatefulWidget {
  const DelAccountScreen({super.key});

  @override
  ConsumerState<DelAccountScreen> createState() => _DelAccountScreenState();
}

class _DelAccountScreenState extends ConsumerState<DelAccountScreen> {
  bool _isAgreed = false; // 同意复选框状态

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: CommonAppBar(title: "注销账号"),
      body: Padding(
        padding: EdgeInsets.only(top: 24.h, left: 16.w, right: 16.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 正文内容
            Text(
              '尊敬的用户：',
              style: context.textTheme.headlineLarge!.copyWith(
                color: context.customColorThemeNonNull.textColor1,
              ),
            ),
            SizedBox(height: 16.h),
            Text.rich(
              TextSpan(
                children: [
                  // 黑色部分
                  TextSpan(
                    text: '如果您确定提交"注销账号"申请，',
                    style: context.textTheme.headlineLarge!.copyWith(
                      color: Colors.black, // 黑色
                      height: 1.5,
                    ),
                  ),
                  // 红色部分
                  TextSpan(
                    text: '将删除账户中的个人数据、解绑关联设备、解绑关联第三方账号，解绑账户下的家庭关系，数据将不可恢复，',
                    style: context.textTheme.headlineLarge!.copyWith(
                      color: Color(0xFFD54941), // 红色
                      height: 1.5,
                    ),
                  ),
                  TextSpan(
                    text: '请谨慎操作。',
                    style: context.textTheme.headlineLarge!.copyWith(
                      color: Colors.black, // 黑色
                      height: 1.5,
                    ),
                  ),
                ],
              ),

              // 禁止自动换行
              overflow: TextOverflow.visible,
            ),

            Spacer(),
            Center(
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  PrimaryCheckBox(value: _isAgreed, onChanged: _toggle),
                  SizedBox(width: 8.w),
                  Text(
                    '我已阅读并同意',
                    style: context.textTheme.bodyMedium!.copyWith(
                      color: context.customColorThemeNonNull.textColor3,
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 24.h),
            PrimaryButton(
              text: "下一步",
              enable: _isAgreed,
              onPressed: () => {_handleNextStep(context, ref)},
            ),
            SizedBox(height: MediaQuery.of(context).padding.bottom + 16.h),
          ],
        ),
      ),
    );
  }

  void _toggle(bool? value) {
    setState(() {
      _isAgreed = value ?? false;
    });
  }

  void _handleNextStep(BuildContext context, WidgetRef ref) async {
    final account = await ref
        .read(authStorageProvider.notifier)
        .getUserAccount();
    if (account == null) return;
    if (context.mounted == false) return;
    VerifyIdentityRoute(account: account, title: "注销账号").push(context);
  }
}
